/* 
直接更新state的多个方法的对象
*/
import Vue from 'vue'

import { GET_ADDRESS, GET_GOODS, GET_SHOPS, GET_USER_INFO, CLEAR_USER_INFO, RECIVE_GOOD, INCREMENT_FOOD_COUNT, DECREMENT_FOOD_COUNT,CLEAR_COUNT_FOODS} from './mutation-types.js'
export default {
    [GET_ADDRESS](state, { address }) {
        state.address = address
    },
    [GET_GOODS](state, { foodsList }) {
        state.categorys = foodsList
    },
    [GET_SHOPS](state, { shops }) {
        state.shops = shops
    },
    [GET_USER_INFO](state, { userInfo }) {
        state.userInfo = userInfo
    },
    [CLEAR_USER_INFO](state) {
        state.userInfo = {};
    },
    [RECIVE_GOOD](state, { shopGood }) {
        state.shopGood = shopGood
    },
    //增加 food.count
    [INCREMENT_FOOD_COUNT](state, { food }) {
        if (!food.count) {
            // food.count=1 //新增属性  没有属性绑定效果 只能 更新数据里的count 不能有数据绑定
            /* 对象 属性名 属性值 */
            Vue.set(food, 'count', 1) //让新增的属性名具有属性绑定
            /* 点击添加 添加进新建的购物车数组 */
            state.countFoods.push(food)
        } else {
            food.count++
        }
    },
    //减少food.count
    [DECREMENT_FOOD_COUNT](state, { food }) {
        if (food.count) {
            food.count--
            if(food.count===0){
                state.countFoods.splice(state.countFoods.indexOf(food),1)
            }
        }
    },
    //清除countFoods 内的数据
    [CLEAR_COUNT_FOODS](state){
        //清除 food的count 
        state.countFoods.forEach(element => {
            element.count=0
        });
        //移除购物车的购物项
        state.countFoods=[];
    }
}